home *** CD-ROM | disk | FTP | other *** search
/ The Best of MacTutor - S…e Code for Volumes 1 to 5 / The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin / Source Code / #45 (Jun 89) / Splitbar Test ƒ / SplitbarTest.Pas < prev    next >
Pascal/Delphi Source File  |  1989-03-28  |  16KB  |  445 lines

  1. program SplitbarTest;
  2. {Program name:SplitbarTest.Pas   }
  3. {Function:  This is a demo application of the Splitbar CDEF (ID = 17).  }
  4. {History: 3/14/89 Original by Prototyper.   }
  5. {Modified to work right: By Kirk Chase }
  6.  
  7.     uses
  8.         Test_Window, MyGlobals, MyScroll;
  9.  
  10.     var
  11.         myEvent: EventRecord;  {Event record for all events}
  12.         doneFlag: boolean;  {Exit program flag}
  13.         code: integer;   {Determine event type}
  14.         SavePort, whichWindow: WindowPtr;   {See which window for event}
  15.         tempRect, GrowRect, DragRect: Rect;  {Rect for dragging}
  16.         mResult: longint;  {Menu list and item selected values}
  17.         theMenu, theItem: integer; {Menu list and item selected}
  18.         chCode: integer; {Key code}
  19.         ch: char; {Key pressed in Ascii}
  20.         IBeam: CursHandle; {IBeam Cursor}
  21.         sleep: integer; {MF sleep period}
  22.         DoIt: boolean;
  23.         ResumePeek: WindowPeek;
  24.         sysResult: boolean;
  25.  
  26.     procedure D_About; {puts up About Box}
  27.         var
  28.             GetSelection: DialogPtr;         {Name of dialog}
  29.             ItemHit: integer;
  30.             ExitDialog: boolean;               {Flag used to exit the Dialog}
  31.  
  32.     begin                               {Start of dialog handler}
  33.         GetSelection := GetNewDialog(AboutDialogID, nil, Pointer(-1)); {Bring in the dialog resource}
  34.         ShowWindow(GetSelection);
  35.         SelectWindow(GetSelection);
  36.         SetPort(GetSelection);
  37.         ExitDialog := FALSE;                {Do not exit dialog handle loop yet}
  38.         repeat                            {Start of dialog handle loop}
  39.             ModalDialog(nil, itemHit);      {Wait until an item is hit}
  40.             if (ItemHit = About_OK) then         {Handle the Button being pressed}
  41.                 begin
  42.                     ExitDialog := TRUE;             {Exit the dialog when this selection is made}
  43.                 end;
  44.         until ExitDialog;
  45.         DisposDialog(GetSelection);       {Flush the dialog out of memory}
  46.     end;  {of D_About}
  47.  
  48.     procedure Init_My_Menus;              {Initialize the menus}
  49.     begin
  50.         ClearMenuBar;                       {Clear any old menu bars}
  51.  
  52.         AppleMenuHandle := GetMenu(AppleMenuID);         {Get the menu from the resource file}
  53.         AddResMenu(AppleMenuHandle, 'DRVR');        {Add in DAs}
  54.         InsertMenu(AppleMenuHandle, 0);            {Insert this menu into the menu bar}
  55.  
  56.         FileMenuHandle := GetMenu(FileMenuID);         {Get the menu from the resource file}
  57.         InsertMenu(FileMenuHandle, 0);            {Insert this menu into the menu bar}
  58.  
  59.         EditMenuHandle := GetMenu(EditMenuID);         {Get the menu from the resource file}
  60.         InsertMenu(EditMenuHandle, 0);            {Insert this menu into the menu bar}
  61.  
  62.         DrawMenuBar;                        {Draw the menu bar}
  63.     end; {of Init_My_Menus}
  64.  
  65.     procedure FixMenus; {adjust menu items such as New and Copy}
  66.     begin
  67.         if (FrontWindow <> nil) then {is there a front window?}
  68.             begin
  69.                 if (FrontWindow <> MyWindow) then {is the front window mine?}
  70.                     begin {somebody else's window is in front}
  71.                         DisableItem(FileMenuHandle, C_New);
  72.                         DisableItem(FileMenuHandle, C_Close);
  73.                         EnableItem(EditMenuHandle, C_Cut);
  74.                         EnableItem(EditMenuHandle, C_Copy);
  75.                         EnableItem(EditMenuHandle, C_Paste);
  76.                     end
  77.                 else {my window is up in front}
  78.                     begin
  79.                         EnableItem(FileMenuHandle, C_Close);
  80.                         DisableItem(FileMenuHandle, C_New);
  81.  
  82.                         DisableItem(EditMenuHandle, C_Undo);
  83.  
  84.                         if (EditText^^.selStart <> EditText^^.selEnd) then {is there an non empty selection?}
  85.                             begin {non empty selection}
  86.                                 EnableItem(EditMenuHandle, C_Cut);
  87.                                 EnableItem(EditMenuHandle, C_Copy);
  88.                             end
  89.                         else
  90.                             begin
  91.                                 DisableItem(EditMenuHandle, C_Cut);
  92.                                 DisableItem(EditMenuHandle, C_Copy);
  93.                             end;
  94.  
  95.                         if (TEGetScrapLen <> 0) then {is there some TE Scrap?}
  96.                             EnableItem(EditMenuHandle, C_Paste) {something there}
  97.                         else
  98.                             DisableItem(EditMenuHandle, C_Paste); {nothing there}
  99.                     end;
  100.             end
  101.         else
  102.             begin {no front window}
  103.                 DisableItem(EditMenuHandle, C_Cut);
  104.                 DisableItem(EditMenuHandle, C_Copy);
  105.                 DisableItem(EditMenuHandle, C_Paste);
  106.                 DisableItem(FileMenuHandle, C_Close);
  107.                 EnableItem(FileMenuHandle, C_New);
  108.             end;
  109.     end; {of FixMenus}
  110.  
  111.     procedure FixCursor; {Non MF Cursor adjust}
  112.         var
  113.             mousept: Point;
  114.             ContentRect: Rect;
  115.     begin
  116.         if FrontWindow = nil then
  117.             InitCursor
  118.         else if FrontWindow = MyWindow then
  119.             begin
  120.                 GetMouse(mousept);
  121.                 ContentRect := MyWindow^.portRect;
  122.                 ContentRect.right := Split^^.contrlRect.left;
  123.                 ContentRect.bottom := ContentRect.bottom - SBarWidth;
  124.                 if (PtInRect(mousePt, ContentRect)) then
  125.                     SetCursor(IBeam^^)
  126.                 else
  127.                     InitCursor;
  128.             end;
  129.     end; {of FixCursor}
  130.  
  131.     procedure Handle_My_Menu (var doneFlag: boolean; theMenu, theItem: integer);  {Handle menu selections}
  132.         var
  133.             DNA: integer;                      {For opening DAs}
  134.             BoolHolder: boolean;               {For SystemEdit result}
  135.             DAName: Str255;                    {For getting DA name}
  136.             SavePort: GrafPtr;                 {Save current port when opening DAs}
  137.  
  138.     begin                               {Start of procedure}
  139.         case theMenu of                 {Do selected menu list}
  140.             AppleMenuID: 
  141.                 begin
  142.                     case theItem of             {Handle all commands in this menu list}
  143.                         About_Splitbar: 
  144.                             begin
  145.                                 D_About;              {Call a dialog for this menu selection}
  146.                             end;
  147.                         otherwise                 {Handle the DAs}
  148.                             begin                   {Start of Otherwise}
  149.                                 GetPort(SavePort);    {Save the current port}
  150.                                 GetItem(AppleMenuHandle, theItem, DAName); {Get the name of the DA selected}
  151.                                 DNA := OpenDeskAcc(DAName); {Open the DA selected}
  152.                                 SetPort(SavePort);    {Restore to the saved port}
  153.                             end;
  154.                     end;                        {End of item case}
  155.                 end;                          {End for this list}
  156.  
  157.             FileMenuID: 
  158.                 begin
  159.                     case theItem of             {Handle all commands in this menu list}
  160.                         C_New: 
  161.                             begin
  162.                                 Open_Test_Window;     {Call a window for this menu selection}
  163.                             end;
  164.                         C_Close: 
  165.                             begin
  166.                                 Close_Test_Window(MyWindow);
  167.                             end;
  168.                         C_Quit: 
  169.                             begin
  170.                                 doneFlag := TRUE;
  171.                             end;
  172.                     end;                        {End of item case}
  173.                 end;                          {End for this list}
  174.  
  175.             EditMenuID: 
  176.                 begin
  177.                     BoolHolder := SystemEdit(theItem - 1); {Do DA editing}
  178.                     if not (BoolHolder) then    {If not a DA then we get it}
  179.                         begin                     {Handle by using a Case statment}
  180.                             case theItem of           {Handle all commands in this menu list}
  181.                                 C_Undo: 
  182.                                     begin
  183.                                     end;
  184.                                 C_Cut: 
  185.                                     begin
  186.                                         ScrollToSelection;
  187.                                         TECut(EditText);    {Handle a Cut in a TE area}
  188.                                         AdjustScrollBars;
  189.                                         AdjustText(1);
  190.                                         AdjustText(2);
  191.                                         UpdateOtherPane;
  192.                                         ScrollToSelection;
  193.                                     end;
  194.                                 C_Copy: 
  195.                                     begin
  196.                                         ScrollToSelection;
  197.                                         TECopy(EditText);    {Handle a Copy in a TE area}
  198.                                         UpdateOtherPane;
  199.                                         ScrollToSelection;
  200.                                     end;
  201.                                 C_Paste: 
  202.                                     begin
  203.                                         ScrollToSelection;
  204.                                         TEPaste(EditText);     {Handle a Paste in a TE area}
  205.                                         AdjustScrollBars;
  206.                                         AdjustText(1);
  207.                                         AdjustText(2);
  208.                                         UpdateOtherPane;
  209.                                         ScrollToSelection;
  210.                                     end;
  211.                             end;                      {End of item case}
  212.                         end;                      {End of not BoolHolder}
  213.                 end;                          {End for this list}
  214.         end;                              {End for lists}
  215.  
  216.         HiliteMenu(0);    {Turn menu selection off}
  217.     end; {of Handle_My_Menu}
  218.  
  219.     procedure InitMac; {Initialize Mac Stuff}
  220.         var
  221.             MPtr: ^integer;
  222.     begin
  223.         MoreMasters;
  224.         InitGraf(@thePort);
  225.         InitFonts;
  226.         InitWindows;
  227.         InitMenus;
  228.         TEInit;
  229.         InitDialogs(nil);
  230.         FlushEvents(everyEvent, 0);
  231.         InitCursor;
  232.  
  233.         MPtr := pointer(mbarHeightGlobal);
  234.         mBarHeight := MPtr^;
  235.     end; {of InitMac}
  236.  
  237.     procedure InitApp; {Initialize Application Stuff}
  238.     begin
  239.         doneFlag := FALSE;  {Do not exit program yet}
  240.         Init_My_Menus;  {Initialize menu bar}
  241.         IBeam := GetCursor(IBeamCursor); {Get IBeam Cursor}
  242.  
  243.         DragRect := screenbits.bounds; {set drag rect}
  244.         InsetRect(DragRect, 10, 10);
  245.         DragRect.top := DragRect.top + mBarHeight;
  246.  
  247.         theErr := SysEnvirons(1, theWorld);
  248.         if (theWorld.machineType >= 0) and (NGetTrapAddress(WNETrapNum, ToolTrap) = NGetTrapAddress(UnImplTrapNum, ToolTrap)) then
  249.             WNE := false
  250.         else
  251.             WNE := true;
  252.         sleep := 10;
  253.  
  254.         if TEFromScrap <> noErr then {get TE Scrap from Scrap Handler}
  255.             TESetScrapLen(0);
  256.  
  257.         EditText := nil;    {Init EditText TE Record}
  258.         MyWindow := nil;         {Initialize the window}
  259.     end; {of InitApp}
  260.  
  261. {===================================================}
  262. begin    {of main program}
  263.     InitMac;
  264.     InitApp;
  265.  
  266.     Open_Test_Window;                   {Open the window routines at program start}
  267.  
  268.     repeat                              {Start of main event loop}
  269.         FixCursor;
  270.         FixMenus;
  271.  
  272.         if (EditText <> nil) then {See if a TE is active}
  273.             TEIdle(EditText); {Blink the cursor if everything is ok}
  274.  
  275.         if WNE then
  276.             DoIt := WaitNextEvent(everyEvent, myEvent, sleep, nil)
  277.         else
  278.             begin
  279.                 SystemTask;
  280.                 DoIt := GetNextEvent(everyEvent, myEvent);
  281.             end;
  282.         if DoIt then {If event then...}
  283.             begin                           {Start handling the event}
  284.                 code := FindWindow(myEvent.where, whichWindow); {Get which window the event happened in}
  285.  
  286.                 case myEvent.what of            {Decide type of event}
  287.                     MouseDown:                   {Mouse button pressed}
  288.                         begin                       {Handle the pressed button}
  289.                             if (code = inMenuBar) then {See if a menu selection}
  290.                                 begin                   {Get the menu selection and handle it}
  291.                                     mResult := MenuSelect(myEvent.Where); {Do menu selection}
  292.                                     theMenu := HiWord(mResult); {Get the menu list number}
  293.                                     theItem := LoWord(mResult); {Get the menu list item number}
  294.                                     Handle_My_Menu(doneFlag, theMenu, theItem); {Handle the menu}
  295.                                 end; {of inMenuBar}
  296.  
  297.                             if (code = InDrag) then   {See if in a window drag area}
  298.                                 begin                   {Do dragging the window}
  299.                                     DragWindow(whichWindow, myEvent.where, DragRect); {Drag the window}
  300.                                 end; {of InDrag}
  301.  
  302.                             if (code = inGrow) then   {In a grow area of the window}
  303.                                 begin                   {Handle the growing}
  304.                                     EraseRect(Split^^.contrlRect);    {Erase the old bottom edge}
  305.  
  306.                                     SetRect(GrowRect, 55, 55 + Split^^.contrlValue, 1000, 1000);  {Min Horz size, Min Vert size, Max Horz size, Max Vert size}
  307.                                     mResult := GrowWindow(whichWindow, myEvent.where, GrowRect); {Grow it}
  308.                                     SizeWindow(whichWindow, LoWord(mResult), HiWord(mResult), TRUE); {Resize to result}
  309.                                     FixScrollbarRects;
  310.                                     DrawGrowIcon(whichWindow); {Draw the grow Icon again}
  311.                                     AdjustScrollBars;
  312.                                     DrawControls(MyWindow);
  313.                                     AdjustText(1);
  314.                                     AdjustText(2);
  315.                                     ScrollToSelection;
  316.                                 end; {of doing the growing}
  317.  
  318.                             if (code = inGoAway) then {See if in a window goaway area}
  319.                                 begin                   {Handle the goaway button}
  320.                                     if TrackGoAway(whichWindow, myEvent.where) then {See if mouse released in GoAway box}
  321.                                         begin                 {Handle the GoAway}
  322.                                             Close_Test_Window(MyWindow);
  323.                                         end;                  {End of TrackGoAway}
  324.                                 end; {of InGoAway}
  325.  
  326.                             if (code = inContent) then {See if in a window}
  327.                                 begin                   {Handle the hit inside a window}
  328.                                     if (whichWindow <> FrontWindow) then {See if already selected or not, in front if selected}
  329.                                         SelectWindow(whichWindow) {Select this window to make it active}
  330.                                     else                    {If already in front the already selected}
  331.                                         begin                 {Handle the button in the content}
  332.                                             SetPort(whichWindow); {Get ready to draw in this window}
  333.                                             Do_Test_Window(myEvent); {Handle this window}
  334.                                         end;                  {End of else}
  335.                                 end;                    {End of inContent}
  336.  
  337.                             if (code = inSysWindow) then {See if a DA selection}
  338.                                 SystemClick(myEvent, whichWindow); {Let other programs in}
  339.  
  340.                         end; {of MouseDown}
  341.  
  342.                     KeyDown, AutoKey:              {Handle key inputs}
  343.                         begin                       {Get the key and handle it}
  344.                             with myevent do           {Check for menu command keys}
  345.                                 begin                   {}
  346.                                     chCode := BitAnd(message, CharCodeMask); {Get character}
  347.                                     ch := CHR(chCode);    {Change to ASCII}
  348.                                     if (Odd(modifiers div CmdKey)) then {See if Command key is down}
  349.                                         begin               {}
  350.                                             mResult := MenuKey(ch); {See if menu selection}
  351.                                             theMenu := HiWord(mResult); {Get the menu list number}
  352.                                             theItem := LoWord(mResult); {Get the menu item number}
  353.                                             if (theMenu <> 0) then {See if a list was selected}
  354.                                                 Handle_My_Menu(doneFlag, theMenu, theItem); {Do the menu selection}
  355.  
  356.                                         end                 {}
  357.                                     else if (EditText <> nil) then {}
  358.                                         begin
  359.                                             TEDeactivate(EditText);
  360.                                             TEKey(ch, EditText); {}
  361.                                             TEActivate(EditText);
  362.                                             ScrollToSelection;
  363.                                             UpdateOtherPane;
  364.                                         end;
  365.                                 end;                    {End for with}
  366.                         end; {of KeyDown,AutoKey}
  367.  
  368.                     UpDateEvt:                   {Update event for a window}
  369.                         begin                       {Handle the update}
  370.                             whichWindow := WindowPtr(myEvent.message); {Get the window the update is for}
  371.                             if whichWindow = MyWindow then
  372.                                 begin
  373.                                     GetPort(SavePort);                {Save the current port}
  374.                                     SetPort(MyWindow);                {Set the port to my window}
  375.                                     BeginUpdate(whichWindow); {Set the clipping to the update area}
  376.                                     Update_Test_Window; {Update this window}
  377.                                     EndUpdate(whichWindow);   {Return to normal clipping area}
  378.                                     SetPort(SavePort);                {Restore the old port}
  379.                                 end;
  380.                         end; {of UpDateEvt}
  381.  
  382.                     ActivateEvt:                 {Window activated event}
  383.                         begin                       {Handle the activation}
  384.                             whichWindow := WindowPtr(myevent.message); {Get the window to be activated}
  385.                             if odd(myEvent.modifiers) then {Make sure it is Activate and not DeActivate}
  386.                                 SelectWindow(whichWindow); {Activate the window by selecting it}
  387.                         end; {of ActivateEvt}
  388.  
  389.                     MultiEvt: 
  390.                         begin
  391.                             if Odd(myEvent.message) then
  392.                                 begin {resume event}
  393.                                     if FrontWindow = MyWindow then
  394.                                         begin
  395.                                             SetPort(MyWindow);
  396.                                             InvalRect(MyWindow^.portRect);
  397.                                             FixMenus;
  398.                                         end
  399.                                     else if FrontWindow <> nil then
  400.                                         begin
  401.                                             ResumePeek := WindowPeek(FrontWindow);
  402.                                             if ResumePeek^.windowKind < 0 then
  403.                                                 begin
  404.                                                     myEvent.what := activateEvt;
  405.                                                     BitSet(@myEvent.modifiers, bit0);
  406.                                                     sysResult := SystemEvent(myEvent);
  407.                                                 end;
  408.                                         end;
  409.                                 end {of resume event}
  410.  
  411.                             else {suspend event}
  412.                                 begin
  413.                                     if FrontWindow = MyWindow then
  414.                                         begin
  415.                                             SetPort(MyWindow);
  416.                                             InvalRect(MyWindow^.portRect);
  417.                                             EnableItem(EditMenuHandle, C_Undo);
  418.                                             EnableItem(EditMenuHandle, C_Cut);
  419.                                             EnableItem(EditMenuHandle, C_Copy);
  420.                                             EnableItem(EditMenuHandle, C_Paste);
  421.                                             DrawGrowIcon(MyWindow);
  422.                                         end
  423.                                     else if FrontWindow <> nil then
  424.                                         begin
  425.                                             ResumePeek := WindowPeek(FrontWindow);
  426.                                             if ResumePeek^.windowKind < 0 then
  427.                                                 begin
  428.                                                     myEvent.what := activateEvt;
  429.                                                     BitClr(@myEvent.modifiers, bit0);
  430.                                                     sysResult := SystemEvent(myEvent);
  431.                                                 end;
  432.                                         end;
  433.                                 end;
  434.                         end; {of MultiEvt}
  435.  
  436.                     otherwise                     {Used for debugging, to see what other events are coming in}
  437.                         begin                       {}
  438.                         end;                        {End of otherwise}
  439.  
  440.                 end;                            {End of case}
  441.  
  442.             end;                            {end of GetNextEvent}
  443.     until doneFlag;                     {End of the event loop}
  444.  
  445. end.                                    {End of the program}